<template>
  <a-modal
    :open="open"
    width="640px"
    :title="props.rowData?.roomId ? '修改诊室' : '新增诊室'"
    :ok-button-props="{ disabled: !isCompleted }"
    centered
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '80px' } }">
      <a-form-item label="诊室编号" class="w-1/2" v-bind="validateInfos.roomId">
        <a-input v-model:value="formState.roomId" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="诊室名称" class="w-1/2" v-bind="validateInfos.roomName">
        <a-input v-model:value="formState.roomName" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="诊室地址" class="w-1/2" v-bind="validateInfos.roomAddr">
        <a-input v-model:value="formState.roomAddr" placeholder="请输入" />
      </a-form-item>

      <a-form-item label="顺序号" class="w-1/2" v-bind="validateInfos.outOrder">
        <a-input v-model:value="formState.outOrder" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="诊间屏地址端口" class="w-full" :label-col="{ style: { width: '110px' } }">
        <a-input v-model:value="formState.screenAddr" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="所在候诊区" class="w-full" :label-col="{ style: { width: '80px' } }">
        <!-- <a-input v-model:value="formState.regionId" placeholder="请输入" /> -->
        <a-select v-model:value="formState.regionId" placeholder="请选择">
          <a-select-option v-for="(item, index) in regionData" :key="index" :value="item.id">{{ item.regionName }}</a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="4" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'update:value', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({
  roomId: '',
  roomName: '',
  roomAddr: '',
  outOrder: '',
  screenAddr: '',
  regionId: undefined,
  memo: ''
})
const regionData = ref([])
const rulesState = ref({
  roomId: [{ required: true, message: '请输入诊室编号' }],
  roomName: [{ required: true, message: '请输入诊室名称' }],
  roomAddr: [{ required: true, message: '请输入诊室地址' }],
  outOrder: [{ required: true, message: '请输入顺序号' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData }
      getRegionData()
    }
  }
)

const onOk = async () => {
  const res = await post('/operation/opex/doSaveRoomnfo', formState.value)
  if (res.code === 0) {
    message.success(res.msg)
    emit('success')
  } else {
    message.error(res.msg)
  }
}

const onCancel = () => {
  resetFields()
  emit('update:open', false)
}
const getRegionData = async () => {
  const res = await post('/outp/dictRegion/getRegionList', {})
  if (res.code !== 0) message.error(res.msg)
  regionData.value = res.data || []
}
</script>
<style lang="less" scoped></style>
